package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.HyrjMachineFaults;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.HyrjReport;
import com.ruoyi.system.service.IHyrjReportService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 举报管理，用于记录和处理用户的举报Controller
 *
 * @author ruoyi
 * @date 2024-03-08
 */
@RestController
@RequestMapping("/system/report")
public class HyrjReportController extends BaseController {
    @Autowired
    private IHyrjReportService hyrjReportService;
    @Autowired
    private ISysUserService iSysUserService;
    @Autowired
    private ISysRoleService iSysRoleService;

    /**
     * 查询举报管理，用于记录和处理用户的举报列表
     */
    @PreAuthorize("@ss.hasPermi('system:report:list')")
    @GetMapping("/list")
    public TableDataInfo list(HyrjReport hyrjReport) {


        List<SysUser> sysUsers = iSysUserService.selectUserList(new SysUser());

        Map<Long, List<SysUser>> groupedById = sysUsers.stream()
                .collect(Collectors.groupingBy(SysUser::getUserId));

        List<String> roleKeyList = new ArrayList<>();
        SysUser sysUser = iSysUserService.selectUserById(getUserId());
        if (null != sysUser) {
            List<Long> longs = iSysRoleService.selectRoleListByUserId(sysUser.getUserId());
            for (Long aLong : longs) {
                SysRole sysRole = iSysRoleService.selectRoleById(aLong);
                if (null != sysRole) {
                    roleKeyList.add(sysRole.getRoleKey());
                }
            }
        }
        if (!roleKeyList.contains("amdin")) {
            hyrjReport.setReporterId(getUserId());
            hyrjReport.setReportedPersonId(getUserId());
        }


        startPage();

        List<HyrjReport> list = hyrjReportService.selectHyrjReportList(hyrjReport);

        for (HyrjReport report : list) {

            List<SysUser> sysUsers1 = groupedById.get(report.getReporterId());
            if (null != sysUsers1) {
                report.setReporterName(sysUsers1.get(0).getUserName());
            }

            List<SysUser> sysUsers2 = groupedById.get(report.getReportedPersonId());
            if (null != sysUsers2) {
                report.setReportedPersonName(sysUsers2.get(0).getUserName());
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出举报管理，用于记录和处理用户的举报列表
     */
    @PreAuthorize("@ss.hasPermi('system:report:export')")
    @Log(title = "举报管理，用于记录和处理用户的举报", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HyrjReport hyrjReport) {
        List<HyrjReport> list = hyrjReportService.selectHyrjReportList(hyrjReport);
        ExcelUtil<HyrjReport> util = new ExcelUtil<HyrjReport>(HyrjReport.class);
        util.exportExcel(response, list, "举报管理，用于记录和处理用户的举报数据");
    }

    /**
     * 获取举报管理，用于记录和处理用户的举报详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:report:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(hyrjReportService.selectHyrjReportById(id));
    }

    /**
     * 新增举报管理，用于记录和处理用户的举报
     */
    @PreAuthorize("@ss.hasPermi('system:report:add')")
    @Log(title = "举报管理，用于记录和处理用户的举报", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HyrjReport hyrjReport) {
        hyrjReport.setReporterId(getUserId());
        hyrjReport.setCreateBy(String.valueOf(getUserId()));
        return toAjax(hyrjReportService.insertHyrjReport(hyrjReport));
    }

    /**
     * 修改举报管理，用于记录和处理用户的举报
     */
    @PreAuthorize("@ss.hasPermi('system:report:edit')")
    @Log(title = "举报管理，用于记录和处理用户的举报", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HyrjReport hyrjReport) {
        return toAjax(hyrjReportService.updateHyrjReport(hyrjReport));
    }

    /**
     * 删除举报管理，用于记录和处理用户的举报
     */
    @PreAuthorize("@ss.hasPermi('system:report:remove')")
    @Log(title = "举报管理，用于记录和处理用户的举报", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(hyrjReportService.deleteHyrjReportByIds(ids));
    }
}
